import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {ParamConfig} from '../common/param.config';
import 'rxjs/add/operator/toPromise';
import {Authority} from './navbar';
import {UserService} from '../user/user.service';
import {Router} from '@angular/router';

@Injectable()
export class NavbarService {
  private hostUrlmenu = ParamConfig.HTTP_WEB_HOST_URL + 'resource/getTree'; // URL to web api
  private hostUrllogout = ParamConfig.HTTP_WEB_HOST_URL + 'user/logout'; // URL to web api
  authorities: Authority[] = null;
  public token: string;
  constructor(private http: Http, private userService: UserService, private router: Router) {}
// 获取菜单
  getRobot(): Promise<[boolean, any]> {
    this.token = this.userService.getToken();
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('token', this.userService.getToken());
    const request = JSON.stringify({
      type: 'robot'
    });
    const requestBody = 'json=' + request;
    return this.http.post(this.hostUrlmenu, requestBody, {headers: headers})
      .toPromise()
      .then(res => {
        const body = res.json();
        if (body.status === 1) {
          this.authorities = body.userTreeTo as Authority[];
          return [true, body];
        }
        if (body.status === 2) {
          this.router.navigate(['/login']);
        }
      })
      .catch(this.handleError);
  }

  getApplication(): Promise<[boolean, any]> {
    this.token = this.userService.getToken();
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    const request = JSON.stringify({
      type: 'application'
    });
    const requestBody = 'json=' + request;
    return this.http.post(this.hostUrlmenu, requestBody, {headers: headers})
      .toPromise()
      .then(res => {
        const body = res.json();
        if (body.status === 1) {
          this.authorities = body.userTreeTo as Authority[];
          return [true, body];
        }
        if (body.status === 2) {
          alert(body.msg);
          this.router.navigate(['/login']);
        };
      })
      .catch(this.handleError);
  }

  getUser(): Promise<[boolean, any]> {
    this.token = this.userService.getToken();
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    const request = JSON.stringify({
      type: 'user'
    });
    const requestBody = 'json=' + request;
    return this.http.post(this.hostUrlmenu, requestBody, {headers: headers})
      .toPromise()
      .then(res => {
        const body = res.json();
        if (body.status === 1) {
          this.authorities = body.userTreeTo as Authority[];
          return [true, body];
        }
        if (body.status ===  2) {
          alert(body.msg);
          this.router.navigate(['/login']);
        }
      })
      .catch(this.handleError);
  }

// 退出登录
  logout(): Promise<[boolean, any]> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    const requestBody = 'token=' + this.token;
    return this.http.post(this.hostUrllogout, requestBody
      , {headers: headers})
      .toPromise()
      .then(res => {
        const body = res.json();
        if (body.status === 1) {
          this.authorities = body.userTreeTo as Authority[];
          return [true, body];
        }
        if (body.status === 2) {
          alert(body.msg);
          this.router.navigate(['/login']);
        }
      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
